import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class NumArrayListTest {
    public static void main(String[] args){
        /**
         * 增删改查
         */
        ArrayList<Integer> baseList = new ArrayList<>();

        // ======== 添加元素
        baseList.add(10);
        baseList.add(20);
        baseList.add(30);

        // ======== 插入元素，在索引 1 处添加 15
        baseList.add(1, 15);

        // ======== 修改元素，修改 索引 2
        baseList.set(2, 25);

        // ======== 删除元素，删除值为 15 的
        baseList.remove(Integer.valueOf(15));

        // ======== 遍历列表
        baseList.forEach(System.out::println);

        // ======== 获取元素，获取索引为 1 的
        System.out.println("Element at index 1: " + baseList.get(1));

        // ======== 检查是否包含某个元素
        System.out.println("Contains 25? " + baseList.contains(25));

        // ======== 获取大小
        System.out.println("Size of list: " + baseList.size());

        /**
         * array 和 list 相互转换
         */
        // 将 Integer[] 转换为 int[]
        Integer[] integerArray = {1, 2, 3, 4, 5};
        int[] intArray = Arrays.stream(integerArray).mapToInt(Integer::intValue).toArray();
        System.out.println(intArray);

        // 将 Integer[] 转换为 List<Integer>
        Integer[] integerArray2 = {1, 2, 3, 4, 5};
        List<Integer> list = Arrays.asList(integerArray2);
        System.out.println(list);

        // 将 Integer[] 转换为流（Stream）
        Integer[] integerArray3 = {1, 2, 3, 4, 5};
        Stream<Integer> stream = Arrays.stream(integerArray3);

        // 将 int[] 转成 List<Integer>
        int[] nums = new int[]{1,3,4};
        List<Integer> list2 = Arrays.asList(Arrays.stream(nums).boxed().toArray(Integer[]::new));
        Collections.sort(list2);


        /**
         * stream 练习
         */
        ArrayList<Integer> streamList = new ArrayList<>();
        streamList.add(10);
        streamList.add(20);
        streamList.add(30);
        streamList.add(10);

        // 1. 求和
        int sum = streamList.stream().mapToInt(Integer::intValue).sum();
        System.out.println("Sum: " + sum);

        // 2. 排序（自然排序）
        List<Integer> sortedList = streamList.stream()
                .sorted()
                .collect(Collectors.toList());
        System.out.println("Sorted List (Ascending): " + sortedList);

        // 3. 排序（自定义排序：倒序）
        List<Integer> sortedListDesc = streamList.stream()
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.toList());
        System.out.println("Sorted List (Descending): " + sortedListDesc);

        // 4. 过滤出大于 15 的元素
        List<Integer> filteredList = streamList.stream()
                .filter(n -> n > 15)
                .collect(Collectors.toList());
        System.out.println("Filtered List (> 15): " + filteredList);

        // 5. 将每个元素平方
        List<Integer> squaredList = streamList.stream()
                .map(n -> n * n)
                .collect(Collectors.toList());
        System.out.println("Squared List: " + squaredList);

        // 6. 求最大值
        Optional<Integer> max = streamList.stream().max(Integer::compareTo);
        max.ifPresent(value -> System.out.println("Max: " + value));

        // 7. 求最小值
        Optional<Integer> min = streamList.stream().min(Integer::compareTo);
        min.ifPresent(value -> System.out.println("Min: " + value));

        // 8. 统计信息，计数，和，平均值，最大值，最小值
        IntSummaryStatistics stats = streamList.stream()
                .collect(Collectors.summarizingInt(Integer::intValue));
        System.out.println("Count: " + stats.getCount());
        System.out.println("Sum: " + stats.getSum());
        System.out.println("Average: " + stats.getAverage());
        System.out.println("Max: " + stats.getMax());
        System.out.println("Min: " + stats.getMin());

        // 9. 去重
        List<Integer> distinctList = streamList.stream()
                .distinct()
                .collect(Collectors.toList());
        System.out.println("Distinct List: " + distinctList);

        // 10. 将元素连接成字符串
        String joinedString = streamList.stream()
                .map(String::valueOf)
                .collect(Collectors.joining(", "));
        System.out.println("Joined String: " + joinedString);

        // 11. 遍历列表（forEach）
        System.out.println("Elements:");
        streamList.stream().forEach(System.out::println);
    }

}
